var UnreadCount = 0;
var oldUnreadCount = 0;
var newUnreadCount = 0;
var text = '';

function load() {
	// Run this periodically so we can poll for new blips.
	window.setInterval(function() {
		newUnreadCount = getUnreadCount();

		if(newUnreadCount != UnreadCount) {
			oldUnreadCount = UnreadCount;

			// Set our variable so we don't keep alerting everyone.
			UnreadCount = newUnreadCount;

			// If there are more messages then there used to be, let's alert the user.
			if(newUnreadCount > oldUnreadCount) {

				text = 'You have ' + UnreadCount + ' waves with new blips.';
				if(UnreadCount == 1) {
					text = 'You have ' + UnreadCount + ' wave with new blips.';
				}
				window.platform.getAttention();
				window.platform.showNotification('New Blips', text, null);
			}
		}
	}, 1000);
}

// Fetch the unread count based on what the document title says.
function getUnreadCount() {
	var match = window.content.document.title.match(/\(([0-9]+)\)/);
	return match ? match[1] : 0;
}